<template>
  <div>
    <e-table
      @getList="handleGetList"
      :tableData="tableData"
      :columns="columns"
      :loading="loading"
      :pageObj="null"
      :border="null"
      @row-click="handleRowClick"
    >
      <el-table-column
        slot="order"
        label="序号"
        type="index"
        :index="indexMethod"
        width="60"
        align="center"
      />
      <el-table-column slot="sort" label="排序" width="100" align="center">
        <template slot-scope="scope">
          <el-input
            style="width: 65px"
            :max="100"
            size="mini"
            v-model="scope.row.areaOrder"
            @blur="handleOrderChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column slot="positions" label="所设岗位" align="center">
        <template slot-scope="scope">
          <span>{{ handlePositionsShow(scope.row.positions) }}</span>
        </template>
      </el-table-column>
      <el-table-column slot="state" width="80" align="center" label="状态">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.state === 0">正常</el-tag>
          <el-tag type="danger" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column slot="action" fixed="right" width="50" align="center">
        <template slot-scope="scope">
          <el-dropdown>
            <a class="el-dropdown-link">
              <i class="el-icon-menu"></i>
            </a>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleEdit(true, scope.row)">
                <a>编辑</a>
              </el-dropdown-item>
              <el-dropdown-item @click.native="handleDelete(scope.row)">
                <a class="red">删除</a>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </e-table>
  </div>
</template>

<script>
import { areaList, deleteArea } from '@/api/department'
import { openNotification, orderChange } from '@/utils/util'

export default {
  data() {
    return {
      loading: false,
      tableData: [], // 表格数据
      // 查询参数
      queryParam: {
        id: '',
        pageNo: 1,
        pageSize: 10
      },
      // 表头
      columns: [
        {
          slot: 'order'
        },
        {
          label: '区域名称',
          prop: 'areaName'
        },
        {
          slot: 'sort'
        },
        {
          slot: 'action'
        }
      ],
      total: 1,
      permissionName: 'system',
      positionArr: [],
      selectObj: {}
    }
  },
  mounted() {
    this.handleGetList()
  },
  methods: {
    indexMethod(index) {
      return (this.queryParam.pageNo - 1) * this.queryParam.pageSize + index + 1
    },
    handleRowClick(row, column, event) {
      console.log('row', row)
      let obj = {}
      obj.parentId = row.areaId
      obj.parentName = row.areaName
      this.$emit('setTree', obj)
    },
    getId(obj) {
      this.selectObj = obj
      this.queryParam.id = obj.parentId
      this.queryParam.pageNo = 1
      this.handleGetList()
    },
    async handleOrderChange(row) {
      let uri = process.env.NODE_ENV === 'production' ? '/area' : '/yb/area'
      const res = await orderChange(row, 'areaOrder', 'areaId', '', uri)
      console.log('res', res)
      if (res.state === '000000') {
        this.handleGetList()
      }
    },
    handleEdit(bool, record) {
      const { parentName, parentId } = this.selectObj
      this.$nextTick(() => {
        const obj = Object.assign(record, { parentName, parentId })
        this.$emit('edit', bool, obj)
      })
    },
    // 删除
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除[${row.areaName}]这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await deleteArea(row.areaId, {})
          openNotification('success', '删除成功')
          _this.$emit('refresh')
        }
      })
    },
    // 处理表格数据显示
    handlePositionsShow(str) {
      if (str) {
        // arr 是个数组
        const arr = JSON.parse(str)
        let arr1 = []
        arr.map(v => {
          arr1.push(v.positionName)
        })
        return arr1.join(',')
      }
    },
    async handleGetList(param = {}) {
      this.loading = true
      this.queryParam = Object.assign(this.queryParam, param)
      const res = await areaList(this.queryParam)
      if (res.data && res.data.length > 0) {
        const { state, data } = res
        this.tableData = data
        // this.total = total
      } else {
        this.tableData = []
        // this.total = 1
      }
      this.loading = false
    }
  }
}
</script>
